package com.zfzs.post.modules.agent.service.impl;

import com.zfzs.post.modules.agent.domain.AgentVerified;
import com.zfzs.post.modules.agent.repository.AgentVerifiedRepository;
import com.zfzs.post.modules.agent.service.AgentVerifiedService;
import com.zfzs.post.modules.agent.service.dto.AgentVerifiedDTO;
import com.zfzs.post.modules.agent.service.dto.AgentVerifiedQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentVerifiedMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
* @author ck
* @date 2019-09-05
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentVerifiedServiceImpl implements AgentVerifiedService {

    @Autowired
    private AgentVerifiedRepository agentVerifiedRepository;

    @Autowired
    private AgentVerifiedMapper agentVerifiedMapper;

    @Override
    public Object queryAll(AgentVerifiedQueryCriteria criteria, Pageable pageable){
        Page<AgentVerified> page = agentVerifiedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentVerifiedMapper::toDto));
    }

    @Override
    public Object queryAll(AgentVerifiedQueryCriteria criteria){
        return agentVerifiedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
    }

    @Override
    public AgentVerified findById(Long id) {
        Optional<AgentVerified> agentVerified = agentVerifiedRepository.findById(id);
        ValidationUtil.isNull(agentVerified,"AgentVerified","id",id);
        return agentVerified.get();
    }

    @Override
    public AgentVerified findByAgentId(Long id) {
        Optional<AgentVerified> agentVerified = agentVerifiedRepository.findById(id);
        return agentVerified.isPresent()?agentVerified.get():null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentVerified create(AgentVerified resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return agentVerifiedRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentVerified resources) {
        Optional<AgentVerified> optionalAgentVerified = agentVerifiedRepository.findById(resources.getId());
        AgentVerified agentVerified = optionalAgentVerified.get();
        agentVerified.copy(resources);
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        agentVerifiedRepository.save(agentVerified);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentVerifiedRepository.deleteById(id);
    }

    @Override
    public List<AgentVerified> findByName(String name) {
        return agentVerifiedRepository.findByCardName(name);
    }
}